function recursionDataTree(dataList, pid, level=1) {
    let currentLevel = level;
    let resultList = [];
    if (!dataList) return null;
    for (const map of dataList) {
        let bmid_new = map["id"];
        let parentId = map["pid"];
        if (pid===parentId) {
            const data = map;
            let [parent] = dataList.filter(item => item["id"] === pid);
            data["parent"] = parent;
            data["level"] = currentLevel;
            let childrenList = recursionDataTree(dataList, bmid_new, currentLevel + 1);
            if (childrenList)
                data["children"]= childrenList;
            resultList.push(data);
        }
    }
    return resultList;
}

function filterDataTree(dataList, name) {
    for (const map of dataList) {
        if (map.name === name){
            return map;
        }
        if (map.children && map.children.length > 0) {
            const foundNode = filterDataTree(map.children, name)
            if (foundNode) {
                return foundNode;
            }
        }
    }
    return null;
}

function iterParent(node, breadcrumbList) {
    if (node.parent) {
        iterParent(node.parent, breadcrumbList);
    }
    breadcrumbList.push(node);
}

function iterDataTree(dataList, headingList, displayList) {
    for (const map of dataList) {
        headingList.push(map.name);
        displayList.push("  ".repeat(map.level-1) + `${map.id} ${map.name}`);
        // displayList.push(displayFunc(map));
        if (map.children && map.children.length > 0) {
            iterDataTree(map.children, headingList, displayList);
        }
    }
}

function getTextLine(doc, text) {
    for (let i = 0; i < doc.lineCount(); i++) {
        let theLine = doc.getLine(i);
        if (theLine.trim() === text.trim()) {
            return i;
        }
    }
    return -1;
}

let headings = [
    {id:'1',pid:'0',name:'保险业务知识学习'},
    {id:'2',pid:'0',name:'投资交易业务知识学习'},
    {id:'2.1',pid:'2',name:'投资系统学习'},
    {id:'3',pid:'0',name:'IT知识学习'},
    {id:'4',pid:'0',name:'IT开发笔记'},
    {id:'5',pid:'0',name:'项目运营'},
    {id:'5.1',pid:'5',name:'虚拟资源销售项目'},
    {id:'6',pid:'0',name:'数学知识学习'},
]
let dataTree = recursionDataTree(headings,'0');
let headingList = [];
let displayList = [];
iterDataTree(dataTree, headingList, displayList);

// let dltest=[{id:'1',pid:'0',name:'大类1'},{id:'2',pid:'0',name:'大类2'},{id:'3',pid:'0',name:'大类3'},{id:'4',pid:'1',name:"中类11"},{id:'5',pid:'1',name:"中类12"},{id:'6',pid:'4',name:"小类111"}];

// const choice = await tp.system.suggester(headingsTitle, headings);
const content = "投资交易业务知识学习"; // tp.file.content
const choice = "IT知识学习"; // await tp.system.suggester(headingsTitle, headings);
const editor = app.workspace.activeLeaf.view.editor;
let insertAtLine = editor.getCursor().line;
console.log(`current cursor line: ${insertAtLine}`);
if (choice) {
    let foundNode = filterDataTree(dataTree, choice);
    let breadcrumbList = [];
    iterParent(foundNode, breadcrumbList);
    let output = "";
    for (const node of breadcrumbList) {
        const temp = "#".repeat(node.level) + `## ${node.name.trim()}`;
        if (content.indexOf(temp) === -1) { // JM：表示现在文档中没有过这一层目录
            editor.setCursor(insertAtLine + 1, 0);
            tR += temp.trimRight() + "\n";
        } else {
            // 如果存在heading，那么将cursor定位到这个heading
            insertAtLine = getTextLine(editor, temp);
            console.log(`heading: ${temp}, line: ${insertAtLine}`);
            new Notice(`heading ${node.name.trim()} already exists.`);
        }
    }
    // tR += output.trimRight()
}
// let output = output += "#".repeat(foundNode.level) + `## ${choice.trim()}`;
// while (!foundNode.parent) {
//     output += "#".repeat(foundNode.level) + `## ${choice.trim()}`;
// }

console.log(dataTree)